<?php
function TopUserSearchInfo(&$ID_User,&$TotalSum,$OH_ID,$PAYER_VAT)
{
  @$dbResult=mysql_query("SELECT OH_UserID FROM order_heads WHERE ID=".$OH_ID);
  $sql_array_User = mysql_fetch_object($dbResult);
  
  $ID_User=$sql_array_User->OH_UserID;
  
  $sql_bin= "SELECT OP_Count,OP_DPH,OP_Price FROM order_products WHERE OH_ID=".$OH_ID;
	$bin_source = mysql_query($sql_bin);
	$TotalSum=0;
	while ($sql_array_Prodct = mysql_fetch_object($bin_source))
	{
	   //$TotalSum+=((($sql_array_Prodct->OP_DPH)/100)+1) * $sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count;
		if($PAYER_VAT=="YES")
            //**********************************************************************
			//platca DPH
            $TotalSum+=((($sql_array_Prodct->OP_DPH)/100)+1) * $sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count;
		    //**********************************************************************
		else
			//**********************************************************************
			//neplatca DPH
			$TotalSum+=$sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count;
			//**********************************************************************	
	}
}

function TopUserUnset($ID_User,$TotalSum){

    if(trim($ID_User) == '' or !is_numeric($ID_User) or $ID_User<1) return false;
    if(trim($TotalSum) == '' or !is_numeric($TotalSum) or $TotalSum<=0) return false;

    $sql_dir= "SELECT Top_Count,Top_price,ID_top FROM top_user WHERE (ID_top=".$ID_User.")";
    $sql = mysql_fetch_object(mysql_query($sql_dir));

    $Count_Value=($sql->Top_Count)-(1);
    $TotalSum_Value=($sql->Top_price)-($TotalSum);
    if(!($Count_Value==0 OR $TotalSum_Value<=0))
    {
          $sql="UPDATE top_user SET Top_Count=".$Count_Value.",Top_price=".$TotalSum_Value." WHERE ID_top=".$ID_User;
            mysql_query($sql);

    }else
    {
        $sql="DELETE FROM top_user WHERE ID_top=".$ID_User;
            mysql_query($sql);

    }
}

function TopUserInsert($ID_User,$TotalSum){

    if(trim($ID_User) == '' or !is_numeric($ID_User) or $ID_User<1) return false;
    if(trim($TotalSum) == '' or !is_numeric($TotalSum) or $TotalSum<=0) return false;

    $sql_psel= "SELECT COUNT(*) FROM top_user WHERE ID_top=".$ID_User;
    $sql_array_Prodct = mysql_fetch_row(mysql_query($sql_psel));
    if($sql_array_Prodct[0]!=0)
    {
        $sql_dir= "SELECT Top_Count,Top_price,ID_top FROM top_user WHERE (ID_top=".$ID_User.")";
        $sql = mysql_fetch_object(mysql_query($sql_dir));

        $Count_Value=($sql->Top_Count)+(1);
        $TotalSum_Value=($sql->Top_price)+($TotalSum);

          $sql="UPDATE top_user SET Top_Count=".$Count_Value.",Top_price=".$TotalSum_Value." WHERE ID_top=".$ID_User;
            mysql_query($sql);

    }else
    {
        $sql="INSERT INTO top_user (ID_top,Top_Count,Top_price) VALUES (".$ID_User.",1,".$TotalSum.")";
            mysql_query($sql);

    }

}
function DeleteTopUser($ID_User){
    if(trim($ID_User) == '' or !is_numeric($ID_User) or $ID_User<1) return false;
    $sql="DELETE FROM top_user WHERE ID_top=".$ID_User;
	mysql_query($sql);
    return true;
}
?>